package com.hz.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hz.web.entity.DepartmentEntity;
import com.hz.web.service.IcreditCommitmentService;
import com.hz.web.service.IdepartmentService;

/**
 * 部门controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("department")
public class DepartmentController {

	@Resource
	private IdepartmentService departmentService;
	
	@Resource
	private IcreditCommitmentService creditCommitmentService;
	
	/**
	 * 添加部门信息
	 * @return
	 */
	@RequestMapping("adddDepartment")
	@ResponseBody
	public Map<String, Object> adddDepartment(DepartmentEntity entity){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int row = -1;
		try {
			entity.setIsDel("1");//1是启用  2是删除 默认启用
			row = departmentService.adddDepartment(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("row", row);
		return resultMap;
	}
	/**
	 * 查询部门信息
	 * @param entity
	 * @return
	 */
	@RequestMapping("queryDepartment")
	@ResponseBody
	public Map<String, Object> queryDepartment(DepartmentEntity entity,int page ,int rows){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			List<DepartmentEntity> ls = departmentService.queryDepartment(entity, page, rows);
			
			resultMap.put("rows", ls);
			int count = departmentService.queryDepartmentCount(entity);
			resultMap.put("total", count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
	/**
	 * 查询部门信息  下拉框使用
	 * @param entity
	 * @return
	 */
	@RequestMapping("queryDepartmentCombobox")
	@ResponseBody
	public List<DepartmentEntity> queryDepartmentCombobox(DepartmentEntity entity){
			List<DepartmentEntity> ls = null;
		try {
			entity.setIsDel("1");//只查询显示的  1是显示  2是隐藏
			ls = departmentService.queryDepartment(entity, 0, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ls;
	}
	
	/**
	 * 修改部门信息
	 * @param entity
	 * @return
	 */
	@RequestMapping("updateDepartment")
	@ResponseBody
	public Map<String, Object> updateDepartment(DepartmentEntity entity){
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int row = -1;
		try {
			if(entity!=null){
				row = departmentService.updateDepartment(entity);
				if("3".equals(entity.getType())){
					Map<String, Object> parmsMap = new HashMap<String, Object>();
					parmsMap.put("departmentCode", entity.getId());
					parmsMap.put("departmentName", entity.getDepartmentName());
					creditCommitmentService.updateCreditCommitmentByCode(parmsMap);
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("row", row);
		return resultMap;
	}
	
}
